package pers.hmedical.base.util;

import cn.hutool.core.lang.Snowflake;

/**
 * id生成策略
 *
 * @author wei-xhh
 * @date 2021-06-12
 */
public class SnowflakeUtil {
    private long workId;
    private long dataCenterId;

    public SnowflakeUtil() {
        this(0L, 0L);
    }

    public SnowflakeUtil(long workId, long dataCenterId) {
        this.workId = workId;
        this.dataCenterId = dataCenterId;
    }

    private static class IdGenHolder {
        private static final SnowflakeUtil INSTANCE = new SnowflakeUtil();
    }

    public static SnowflakeUtil get() {
        return IdGenHolder.INSTANCE;
    }

    private final Snowflake Snowflake = new Snowflake(workId, dataCenterId);

    public synchronized long id() {
        return Snowflake.nextId();
    }

    public static Long getId() {
        return SnowflakeUtil.get().id();
    }

}
